package com.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.Email;
import com.entity.EmailContacts;
import com.mapper.EmailContactsMapper;
import com.mapper.sys.EmailMapper;
import com.service.EmailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: EmailServiceImpl
 * @Author: ARongzhe
 * @Date: 2022/04/27 23:46:11
 * @Description: TODO
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    private EmailMapper emailMapper;

    @Resource
    private EmailContactsMapper emailContactsMapper;

    @Override
    public boolean addEmail(Email email) {
        //插入数据
        int insert = emailMapper.insert(email);
        if (insert == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Email> getAllEmail() {
        Wrapper<Email> allEmailWrapper = new QueryWrapper<Email>().orderByDesc("create_time");
        //使用mybatis-plus查询所有邮件
        List<Email> allSendSuccessEmail = emailMapper.selectList(allEmailWrapper);
        return allSendSuccessEmail;
    }

    @Override
    public List<Email> findBymailTo(String mailTo) {
        QueryWrapper<Email> emailWrapper = new QueryWrapper<Email>().like("mail_to", mailTo);
        List<Email> selectByEmailTo = emailMapper.selectList(emailWrapper);
        return selectByEmailTo;
    }

    @Override
    public List<EmailContacts> findContactsByRemarkName(String remarkName) {
        Wrapper<EmailContacts> emailContactsWrapper = new QueryWrapper<EmailContacts>()
                .like("remark_name", remarkName)
                .orderByDesc("create_time");
        List<EmailContacts> emailContacts = emailContactsMapper.selectList(emailContactsWrapper);
        return emailContacts;
    }

    @Override
    public List<EmailContacts> findAllEmailContacts() {
        Wrapper<EmailContacts> emailContactsWrapper = new QueryWrapper<EmailContacts>().orderByDesc("create_time");
        List<EmailContacts> emailContacts = emailContactsMapper.selectList(emailContactsWrapper);
        return emailContacts;
    }

    @Override
    public boolean addEmailContacts(EmailContacts emailContacts) {
        Wrapper<EmailContacts> emailContactsWrapper = new QueryWrapper<EmailContacts>().eq("mail_to", emailContacts.getMailTo());
        //校验数据库中是不是存在该邮箱
        if (emailContactsMapper.selectList(emailContactsWrapper).size() == 0) {
            int insert = emailContactsMapper.insert(emailContacts);
            if (insert == 1) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public boolean deleteEmailContacts(String id) {
        int i = emailContactsMapper.deleteById(id);
        if (i == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean updateEmailContacts(EmailContacts emailContacts) {
        int i = emailContactsMapper.updateById(emailContacts);
        if (i == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean deleteEmail(String id) {
        int deleteMailStatus = emailMapper.deleteById(id);
        if (deleteMailStatus == 1) {
            return true;
        } else {
            return false;
        }
    }


}

